﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperKit
{
    class DirectoryEx {
        public string path;
        public string name;
        public string fullPath;
        public string[] dirs = null;
        public string[] files = null;
        public List<string> DirList { get; }
        public List<string> FileList { get; }

        public DirectoryInfo info;

        public DirectoryEx(string path) {

            this.path = path;
            try {
                info = new DirectoryInfo(path);
                dirs = Directory.GetDirectories(path);
                files = Directory.GetFiles(path);
                DirList = new List<string>(dirs);
                FileList = new List<string>(files);
                name = Path.GetDirectoryName(path);
                fullPath = Path.GetFullPath(path);
            } catch (Exception) {
                DirList = new List<string>();
                FileList = new List<string>();
            }
        }

        private List<string> CreateList(string[] arr) {
            if (arr == null) return new List<string>();
            List<string> list = new List<string>(arr.Length);
            foreach (string i in arr) {
                list.Add(i);
            }
            return list;
        }

        public List<string> Filter(params string[] exts) {
            List<string> files = new List<string>();
            foreach (string i in FileList) {
                if (Kit.IsFileType(new FileInfo(i), exts)) {
                    files.Add(i);
                }
            }
            return files;
        }

        public void Sort() {
            DirList.Sort();
            FileList.Sort();
        }

        public static int GetAllFiles(DirectoryInfo info, List<FileInfo> list, string[] exts) {
            int count = 0;
            FileInfo[] files = null;
            DirectoryInfo[] dirs = null;
            try {
                files = info.GetFiles();
                dirs = info.GetDirectories();
            } catch (DirectoryNotFoundException) {
                
            }
            if (files != null) {
                foreach (var i in files) {
                    if (Kit.IsFileType(i, exts)) {
                        list.Add(i);
                        count++;
                    }
                }
            }
            if (dirs != null) {
                foreach (var i in dirs) {
                    if ((i.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) {
                        count += GetAllFiles(i, list, exts);
                    }
                }
            }            
            return count;
        }

        public static int GetFileCount(DirectoryInfo info, string[] exts) {
            int count = 0;
            FileInfo[] files = null;
            DirectoryInfo[] dirs = null;
            try {
                files = info.GetFiles();
                dirs = info.GetDirectories();
            } catch (DirectoryNotFoundException) {

            }
            if (files != null) {
                foreach (var i in files) {
                    if (Kit.IsFileType(i, exts)) {
                        count++;
                    }
                }
            }
            if (dirs != null) {
                foreach (var i in dirs) {
                    if ((i.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) {
                        count += GetFileCount(i, exts);
                    }
                }
            }
            return count;
        }

        public int DirCount { get { return DirExist ? dirs.Length : 0; } }
        public int FileCount { get { return FileExist ? files.Length : 0; } }
        public bool DirExist { get { return dirs != null && dirs.Length > 0; } }
        public bool FileExist { get { return files != null && files.Length > 0; } }

        public bool Empty { get { return DirExist == false && FileExist == false; } }

    }
}
